﻿#include <iostream>
using namespace std;

int binSearch(int r[], int n, int k){
	int low = 0, high = n - 1;	//设置查找区间，注意数组下标从0开始
	int mid;
	while(low <= high){			//当区间存在时
		mid = (low + high) / 2;
		if(k < r[mid]){
			high = mid - 1;
		} else {
			if(k > r[mid]){
				low = mid + 1;
			} else {
				return mid;		//查找成功，返回元素序号
			}
		}
	}
	
	return 0;					//查找失败，返回0
}

int main(){
	int r[] = {7, 14, 18, 21, 23, 29, 31, 35, 38};
	cout << "折半查找的元素下标为：" <<  binSearch(r, 9, 18) << endl;
	
	system("pause");
	return 0;
}